import 'package:date_format/date_format.dart';

class KYDateFormat{

  static final num ONE_MINUTE = 60000;
  static final num ONE_HOUR = 3600000;
  static final num ONE_DAY = 86400000;
  static final num ONE_WEEK = 604800000;

  static final String ONE_SECOND_AGO = "秒前";
  static final String ONE_MINUTE_AGO = "分钟前";
  static final String ONE_HOUR_AGO = "小时前";
  static final String ONE_DAY_AGO = "天前";
  static final String ONE_MONTH_AGO = "月前";
  static final String ONE_YEAR_AGO = "年前";

//时间转换
  static String chatTimeFormat(String dateStr) {

    //目标时间
    DateTime date = DateTime.parse(dateStr);
    String nowZeroStr = formatDate(DateTime.now(), [yyyy,'-',mm,'-',dd]);
    // LogUtils.d('nowZeroStr======$nowZeroStr');
    //今天时间当天 0点
    DateTime zeroDate = DateTime.parse(nowZeroStr);
    //目标时间过当天时间的
    num todayDelta = DateTime.now().millisecondsSinceEpoch - zeroDate.millisecondsSinceEpoch;
    num delta = DateTime.now().millisecondsSinceEpoch - date.millisecondsSinceEpoch;

    if (delta < todayDelta) {
      //今天
//       if (delta < 1 * ONE_MINUTE) {
//         num seconds = toSeconds(delta);
// //      return (seconds <= 0 ? 1 : seconds).toInt().toString() + ONE_SECOND_AGO;
//         return '刚刚';
//       }

      int minute = date.minute;
      String minuteStr = minute.toString();
      if (minute < 10){
        minuteStr = '0'+minuteStr;
      }
      if (date.hour >= 13){
        return '下午 ' + '${date.hour-12}:'+minuteStr;
      }else{
        return '上午 ' + '${date.hour}:'+minuteStr;
      }

    }else if (delta < todayDelta+ONE_DAY){
      //昨天
      return '昨天';
    }else if (delta < todayDelta+ONE_DAY*6){
      //星期

      int weekDay = date.weekday;
      String weekStr = '';
      if (weekDay == 1){
        weekStr = '星期一';
      }else if(weekDay == 2){
        weekStr = '星期二';
      }else if(weekDay == 3){
        weekStr = '星期三';
      }else if(weekDay == 4){
        weekStr = '星期四';
      }else if(weekDay == 5){
        weekStr = '星期五';
      }else if(weekDay == 6){
        weekStr = '星期六';
      }else if(weekDay == 7){
        weekStr = '星期日';
      }
      return weekStr;

    }else{

      return formatDate(date, [yyyy, '-', mm, '-', dd]);
    }

  }

  static num toSeconds(num date) {
    return date / 1000;
  }

  static num toMinutes(num date) {
    return toSeconds(date) / 60;
  }

  static num toHours(num date) {
    return toMinutes(date) / 60;
  }

  static num toDays(num date) {
    return toHours(date) / 24;
  }

  static num toMonths(num date) {
    return toDays(date) / 30;
  }

  static num toYears(num date) {
    return toMonths(date) / 365;
  }



  static double getDifferWithStartTimeAndEndTime(String startTime,String endTime){
    DateTime startDate = DateTime.parse(startTime);
    DateTime endDate = DateTime.parse(endTime);
    num differ = startDate.millisecondsSinceEpoch - endDate.millisecondsSinceEpoch;
    double dif = differ.toDouble();
    return dif/1000;
  }


  //时间转换
  static String chatCellTimeFormat(String dateStr) {

    //目标时间
    DateTime date = DateTime.parse(dateStr);
    String nowZeroStr = formatDate(DateTime.now(), [yyyy,'-',mm,'-',dd]);
    // LogUtils.d('nowZeroStr======$nowZeroStr');
    //今天时间当天 0点
    DateTime zeroDate = DateTime.parse(nowZeroStr);
    //目标时间过当天时间的
    num todayDelta = DateTime.now().millisecondsSinceEpoch - zeroDate.millisecondsSinceEpoch;
    num delta = DateTime.now().millisecondsSinceEpoch - date.millisecondsSinceEpoch;

    if (delta <= todayDelta) {
      //今天

      int minute = date.minute;
      String minuteStr = minute.toString();
      if (minute < 10){
        minuteStr = '0'+minuteStr;
      }
      if (date.hour >= 13){
        return '下午 ' + '${date.hour-12}:'+minuteStr;
      }else{
        return '上午 ' + '${date.hour}:'+minuteStr;
      }

    }else if (delta < todayDelta+ONE_DAY){
      //昨天
      int minute = date.minute;
      String minuteStr = minute.toString();
      if (minute < 10){
        minuteStr = '0'+minuteStr;
      }
      if (date.hour >= 13){
        return '昨天 下午 ' + '${date.hour-12}:'+minuteStr;
      }else{
        return '昨天 上午 ' + '${date.hour}:'+minuteStr;
      }

    }else if (delta < todayDelta+ONE_DAY*6){
      //星期
      int minute = date.minute;
      String minuteStr = minute.toString();
      if (minute < 10){
        minuteStr = '0'+minuteStr;
      }
      int weekDay = date.weekday;
      String weekStr = '';
      if (weekDay == 1){
        weekStr = '星期一';
      }else if(weekDay == 2){
        weekStr = '星期二';
      }else if(weekDay == 3){
        weekStr = '星期三';
      }else if(weekDay == 4){
        weekStr = '星期四';
      }else if(weekDay == 5){
        weekStr = '星期五';
      }else if(weekDay == 6){
        weekStr = '星期六';
      }else if(weekDay == 7){
        weekStr = '星期日';
      }
      if (date.hour >= 13){
        return weekStr + ' 下午 ' + '${date.hour-12}:'+minuteStr;
      }else{
        return weekStr + ' 上午 ' + '${date.hour}:'+minuteStr;
      }
    }else{

      int minute = date.minute;
      String minuteStr = minute.toString();
      if (minute < 10){
        minuteStr = '0'+minuteStr;
      }
      if (date.hour >= 13){
        return '${date.year}年${date.month}月${date.day}日 下午 '  + '${date.hour-12}:'+minuteStr;
      }else{
        return '${date.year}年${date.month}月${date.day}日 上午 ' + '${date.hour}:'+minuteStr;
      }

      return formatDate(date, [yyyy, '年', mm, '月', dd,'日 ',HH,':',nn,':',ss]);
    }

  }


  static String registerTimeFormat(String dateStr) {

    //目标时间
    DateTime date = DateTime.parse(dateStr);
    return '${date.year}年${date.month}月${date.day}日';

  }

  static String clueTimeFormat(String dateStr) {

    if (dateStr == null || dateStr.length == 0){
      return dateStr;
    }
    dateStr = dateStr.replaceAll('.', '-');
    //目标时间
    DateTime date = DateTime.parse(dateStr);
    String nowZeroStr = formatDate(DateTime.now(), [yyyy,'-',mm,'-',dd]);
    // LogUtils.d('nowZeroStr======$nowZeroStr');
    //今天时间当天 0点
    DateTime zeroDate = DateTime.parse(nowZeroStr);
    //目标时间过当天时间的
    num todayDelta = DateTime.now().millisecondsSinceEpoch - zeroDate.millisecondsSinceEpoch;
    num delta = DateTime.now().millisecondsSinceEpoch - date.millisecondsSinceEpoch;

    if (delta <= todayDelta) {
      //今天
      return formatDate(date, [HH,':',nn,':',ss]);

    }else if (delta < todayDelta+ONE_DAY){
      //昨天
      return formatDate(date, ['昨天 ', HH,':',nn,':',ss]);

    }else{
      return formatDate(date, [yyyy, '-', mm, '-', dd,' ',HH,':',nn,':',ss]);
    }

  }

  static String douYinCommentTimeFormat(String dateStr) {

    if (dateStr == null || dateStr.length == 0){
      return dateStr;
    }
    dateStr = dateStr.replaceAll('.', '-');
    //目标时间
    DateTime date = DateTime.parse(dateStr);
    String nowZeroStr = formatDate(DateTime.now(), [yyyy,'-',mm,'-',dd]);
    // LogUtils.d('nowZeroStr======$nowZeroStr');
    //今天时间当天 0点
    DateTime zeroDate = DateTime.parse(nowZeroStr);
    //目标时间过当天时间的
    num todayDelta = DateTime.now().millisecondsSinceEpoch - zeroDate.millisecondsSinceEpoch;
    num delta = DateTime.now().millisecondsSinceEpoch - date.millisecondsSinceEpoch;

    if (delta <= todayDelta) {
      //今天
      return formatDate(date, [HH,':',nn,]);

    }else if (delta < todayDelta+ONE_DAY){
      //昨天
      return formatDate(date, ['昨天 ', HH,':',nn,]);

    }else{

      //判断是否是今年
      if (dateStr.split('-').first == nowZeroStr.split('-').first){
        return formatDate(date, [mm, '-', dd,' ',HH,':',nn,]);
      }else{
        return formatDate(date, [yyyy, '-', mm, '-', dd,' ',HH,':',nn,]);
      }

    }

  }

  //获取最近7天日期
  static List getNearFutureDatesList(int days){
    List <String>list = [];
    for (int i = days-1; i >= 0; i --){
      num dateNum = DateTime.now().millisecondsSinceEpoch - i * ONE_DAY;
      DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(dateNum.toInt());
      String nowZeroStr = formatDate(dateTime, [mm,'月',dd,'日']);
      list.add(nowZeroStr);
    }
    return list;
  }

  ///大陆手机号码11位数
  static bool isChinaPhoneLegal(String str) {
    //^((13[0-9])|(15[^4])|(166)|(17[0-8])|(18[0-9])|(19[8-9])|(147,145))\\d{8}\$
    return new RegExp('^1[3|4|5|6|7|8|9][0-9]\\d{8}\$').hasMatch(str);
  }



}